<template>
    <div class="app-container">
        <el-tabs v-model="active" type="border-card" @tab-click="tabClick">
            <el-tab-pane label="检查" name="0">
                <nurse-exam-view ref="nurseExamViewRef" :confirm-flag="'1'"  :inorout-flag="'1'" :is-show-query="false"/>
            </el-tab-pane>
            <el-tab-pane label="检验" name="1">
                <nurse-lab-view ref="nurseLabViewRef" :confirm-flag="'1'"  :inorout-flag="'1'" :is-show-query="false"/>
            </el-tab-pane>
            <el-tab-pane label="处方" name="2">
                <nurse-prescription-view ref="nursePrescriptionViewRef" :confirm-flag="'1'"  :inorout-flag="'1'" :is-show-query="false"/>
            </el-tab-pane>
            <el-tab-pane label="医嘱" name="3">
                <el-button class="mb5" style="float: right" type="primary" @click="getDischargedNotDoneOrders()">
                    <template #icon>
                        <i-ep-search/>
                    </template>
                    刷新
                </el-button>
                <el-table class="yizhu-table" v-adaptive="280" :border="true" :data="ordersList" :header-cell-style="$headerCellStyle"
                          :stripe="true"
                          highlight-current-row>
                    <el-table-column :align="'center'" fixed="left" label="组号" prop="groupNumber" width="50">
                        <template #default="scope">
                            {{ scope.row.groupNumber }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" fixed="left" label="长/临" prop="repeatIncaditor" width="60">
                        <template #default="scope">
                            <dict-data-tag :options="dictDataMap['order_type']"
                                           :value="scope.row.repeatIncaditor"/>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" fixed="left" label="医嘱状态" prop="orderStatus" width="90">
                        <template #default="scope">
                            <template v-for="(tag, index) in dictDataMap['order_status']">
                                <template v-if="scope.row.orderStatus.includes(tag.dictValue)">
                                    <el-button v-if="!tag.cssClass" :class="scope.row.orderStatus === '1' ? 'mb25': ''"
                                               :type="tag.listClass"
                                               style="border: 1px #000000 solid">{{ tag.dictLabel }}
                                    </el-button>
                                    <el-button v-else :color="tag.cssClass"
                                               style="border: 1px #000000 solid">{{ tag.dictLabel }}
                                    </el-button>
                                </template>
                            </template>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" fixed="left" label="下达时间" prop="createTime" width="140">
                        <template #default="scope">
                            {{ $parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'left'" fixed="left" label="医嘱类别" prop="orderClass" width="90">
                        <template #default="scope">
                            <dict-data-tag :options="dictDataMap['new_order_type']"
                                           :value="scope.row.orderClass"/>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" fixed="left" label="是否计价" prop="billingAttr" width="90">
                        <template #default="scope">
                            <dict-data-tag :options="dictDataMap['billing_attr']"
                                           :value="scope.row.billingAttr"/>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'left'" :header-align="'center'" :show-overflow-tooltip="true" fixed="left"
                                     label="医嘱内容"
                                     prop="orderText"
                                     width="220">
                        <template #default="scope">
                            {{ scope.row.orderText }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="途径" prop="channel" width="100">
                        <template #default="scope">
                            {{ scope.row.channelName }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="频次" prop="performFreq" width="100">
                        <template #default="scope">
                            {{ scope.row.performFreqName }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="单次用量" prop="numSingle" width="100">
                        <template #default="scope">
                    <span style="margin-left: -15px"> {{ scope.row.numSingle }}<dict-data-tag
                        :options="dictDataMap['spec_unit']" :value="scope.row.minUnits"
                        style="margin: -23px -20px 0px 35px"/></span>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="单次剂量" prop="singleDosage" width="100">
                        <template #default="scope">
                    <span style="margin-left: -15px"> {{ scope.row.singleDosage }}<dict-data-tag
                        :options="dictDataMap['dose_unit']" :value="scope.row.doseUnits"
                        style="margin: -23px -20px 0px 35px"/></span>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="数量" prop="sumNum" width="90">
                        <template #default="scope">
                            <span v-if="!scope.row.unit" style="margin-left: -15px">{{ scope.row.sumNum }}</span>
                            <span v-else style="margin-left: -15px"> {{ scope.row.sumNum }}<dict-data-tag
                                :options="dictDataMap['spec_unit']" :value="scope.row.unit"
                                style="margin: -23px -20px 0px 35px"/></span>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="执行科室" prop="implementDept" width="100">
                        <template #default="scope">
                            {{ scope.row.implementDeptName }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="医嘱说明" prop="remarks" width="120">
                        <template #default="scope">
                            {{ scope.row.remarks }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="开单医生" prop="applyUserName" width="90"/>
                </el-table>
            </el-tab-pane>
            <el-tab-pane label="退药申请" name="4">
                <el-button class="mb5" style="float: right" type="primary" @click="getReturnDrugList()">
                    <template #icon>
                        <i-ep-search/>
                    </template>
                    刷新
                </el-button>
                <el-table v-adaptive="280" :border="true" :data="returnDrugList"
                          :header-cell-style="$headerCellStyle" :stripe="true"
                          default-expand-all highlight-current-row>
                    <el-table-column fixed="left" label="明细" type="expand">
                        <template #default="props">
                            <div m="4">
                                <el-table :border="true" :data="props.row.repercussionApplyVoList" :stripe="true"
                                          highlight-current-row>
                                    <el-table-column fixed="left" width="50px"/>
                                    <el-table-column :align="'center'" :show-overflow-tooltip="true" fixed="left"
                                                     label="药品名称"
                                                     prop="drugName"
                                                     width="130"/>
                                    <el-table-column :align="'center'" label="项目规格" prop="itemSpec" width="140"/>
                                    <el-table-column :align="'center'" label="退药状态"
                                                     prop="returnDrugStatusName"
                                                     width="100">
                                        <template #default="scope">
                                            <dict-data-tag :options="dictDataMap['return_drug_status']"
                                                           :value="scope.row.returnDrugStatus"/>
                                        </template>
                                    </el-table-column>
                                    <el-table-column :align="'center'" label="总数量"
                                                     prop="dispensingAmountShow"
                                                     width="120">
                                        <template #default="scope">
                                        <span
                                            class="ys-css">{{
                                                parseFloat(scope.row.dispensingAmountShow.toString()).toFixed(0)
                                            }}</span>{{ scope.row.unitsName }}
                                        </template>
                                    </el-table-column>
                                    <el-table-column :align="'center'"
                                                     label="已申请退药数量"
                                                     prop="returnAmountShow" width="140">
                                        <template #default="scope">
                                        <span
                                            class="ret-ys-css">{{
                                                parseFloat(scope.row.returnAmountShow.toString()).toFixed(0)
                                            }}</span>{{ scope.row.unitsName }}
                                        </template>
                                    </el-table-column>
                                    <el-table-column :align="'center'" label="退药剩余数量"
                                                     prop="remainingAmountShow"
                                                     width="120">
                                        <template #default="scope">
                                        <span
                                            class="ys-css">{{
                                                parseFloat((scope.row.remainingAmountShow).toString()).toFixed(0)
                                            }}</span>{{ scope.row.unitsName }}
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" fixed="left" header-align="center" label="序号" prop="itemPrice"
                                     width="50">
                        <template #default="scope">
                            {{ scope.$index + 1 }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" fixed="left" label="费用发生时间" prop="medicalVerifyTime"
                                     width="170">
                        <template #default="scope">
                            <span class="ys-css">{{
                                    $parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}')
                                }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" :show-overflow-tooltip="true" fixed="left" label="药品名称"
                                     prop="itemName"/>
                    <el-table-column :align="'right'" header-align="center" label="单价(元)" prop="itemPrice"
                                     width="100">
                        <template #default="scope">
                            <span class="ys-css">{{ parseFloat(scope.row.itemPrice.toString()).toFixed(2) }}</span>元
                        </template>
                    </el-table-column>
                    <el-table-column :align="'right'" header-align="center" label="实收费用(元)" prop="charges"
                                     width="100">
                        <template #default="scope">
                            <span class="ys-css">{{ parseFloat(scope.row.charges.toString()).toFixed(2) }}</span>元
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="项目规格" prop="itemSpec" width="120"/>
                    <el-table-column :align="'center'" label="开单科室" prop="applyDeptName" width="100"/>
                    <el-table-column :align="'center'" label="开单医生" prop="applyDoctorName" width="80"/>
                    <el-table-column :align="'center'" label="执行科室" prop="implementDeptName" width="90"/>
                    <el-table-column :align="'center'" label="患者所在科室" prop="deptStayedName" width="100"/>
                    <el-table-column :align="'center'" label="患者所在病区" prop="wardCodeName" width="100"/>
                    <el-table-column :align="'left'" fixed="left" header-align="center" label="总数量" prop="amount"
                                     width="120">
                        <template #default="scope">
                                <span
                                    class="ys-css">{{
                                        parseFloat(scope.row.amount.toString()).toFixed(0)
                                    }}</span>{{ scope.row.unitsName }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'left'" fixed="left" header-align="center" label="退药数量"
                                     prop="returnAmountInput"
                                     width="135">
                        <template #default="scope">
                            <span>{{ scope.row.returnAmountInput }}&nbsp;{{ scope.row.unitsName }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'left'" fixed="left" header-align="center" label="已申请退药数量"
                                     prop="returnAmountShow" width="80">
                        <template #default="scope">
                                <span
                                    class="ret-ys-css">{{
                                        parseFloat(scope.row.returnAmountShow.toString()).toFixed(0)
                                    }}</span>{{ scope.row.unitsName }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'left'" fixed="left" header-align="center" label="退药剩余数量"
                                     prop="remainingAmountShow"
                                     width="90">
                        <template #default="scope">
                                <span
                                    class="ys-css">{{
                                        parseFloat((scope.row.remainingAmountShow).toString()).toFixed(0)
                                    }}</span>{{ scope.row.unitsName }}
                        </template>
                    </el-table-column>
                </el-table>
            </el-tab-pane>
            <!--            <el-tab-pane label="未摆药" name="5">未摆药</el-tab-pane>-->
        </el-tabs>
    </div>
</template>
<script lang="ts" setup>


import {useUserStoreHook} from "@/store/modules/user.ts";
import {useDictStoreHook} from "@/store/modules/dict.ts";
import {dischargedNotDoneOrders} from "@/api/orders/orders.ts";
import {listDrugRepercussion} from "@/api/hospitalization/inpBillDetail.ts";
import nurseExamView from "@/views/nurse/dischargedNotDoneItem/nurseExamView.vue"
import NurseExamView from "@/views/nurse/dischargedNotDoneItem/nurseExamView.vue"
import nurseLabView from "@/views/nurse/dischargedNotDoneItem/nurseLabView.vue"
import nursePrescriptionView from "@/views/nurse/dischargedNotDoneItem/nursePrescriptionView.vue"

const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
const dictStore = useDictStoreHook();
let dictDataMap = ref(
    dictStore.isTypeGetData("charge_flag_dict", 'dose_unit', 'spec_unit',
        'charge_flag_dict', 'order_type', 'new_order_type', 'order_status', 'billing_attr', 'return_drug_status'
    )
);
const nurseExamViewRef = ref()
const nurseLabViewRef = ref()
const nursePrescriptionViewRef = ref()

const ordersList = ref([]);
const returnDrugList = ref([]);
const active = ref('0')
const queryParams = ref<any>({
    patientId: "",
    visitId: "",
    orgId: userInfo.orgId,
    name: "",
    itemName: '',
    inoroutFlag: '1',
    confirmFlag: '1'
})

/**
 * 父组件加载初始化方法
 */
function initOpen(obj: any) {
    active.value = '0'
    queryParams.value.visitId = obj.visitId;
    queryParams.value.patientId = obj.patientId;
    nurseExamViewRef.value.getExamApplyList(queryParams.value.visitId)
}


/**
 * tab页点击事件
 * @param obj
 */
function tabClick(obj: any) {
    const objThis = toRaw(obj)
    if (objThis.props.name === '0') {
        nurseExamViewRef.value.getExamApplyList(queryParams.value.visitId)
    } else if (objThis.props.name === '1') {
        nurseLabViewRef.value.getLabApplyList(queryParams.value.visitId)
    } else if (objThis.props.name === '2') {
        nursePrescriptionViewRef.value.getPrescriptionApplyList(queryParams.value.visitId)
    } else if (objThis.props.name === '3') {
        getDischargedNotDoneOrders()
    } else if (objThis.props.name === '4') {
        getReturnDrugList()
    }
}

/**
 * 查询医嘱列表
 */
function getDischargedNotDoneOrders() {
    const param = {
        patientId: queryParams.value.patientId,
        visitId: queryParams.value.visitId,
        orgId: queryParams.value.orgId
    }
    dischargedNotDoneOrders(param).then((response: any) => {
        ordersList.value = response.data;
        ordersList.value.forEach((item: any) => {
            item.unit = item.packSize === "1" ? item.minUnits : item.packageUnits
        })
    })
}

function getReturnDrugList() {
    queryParams.value.isReturnOk = "false"
    listDrugRepercussion(queryParams.value).then((response: any) => {
        returnDrugList.value = []
        returnDrugList.value = response.data.filter(e => e.returnAmount > 0)
        returnDrugList.value.forEach((item: any) => {
            // 主记录 退药后剩余数量显示   如果是医嘱开的是大包装，除以包装数量
            // remainingAmount  退药后剩余数量 默认为小包装
            item.remainingAmountShow = item.packSize === '2' ? item.remainingAmount / item.packageNum : item.remainingAmount
            // 主记录 退药数量显示 如果是医嘱开的是大包装，除以包装数量
            // returnAmount 退药数量 默认为小包装
            item.returnAmountShow = item.packSize === '2' ? item.returnAmount / item.packageNum : item.returnAmount
            // 主记录 退药后剩余数量显示   开药数量- 退药数量
            item.remainingAmountShow = item.amount - item.returnAmountShow
            item.returnAmountInput = 0
            if (item.repercussionApplyVoList) {
                item.repercussionApplyVoList.forEach((itemRepercussionApply: any) => {
                    // 明细 退药后剩余数量显示   如果是医嘱开的是大包装，除以包装数量
                    // 明细 remainingAmount  退药后剩余数量 默认为小包装
                    itemRepercussionApply.remainingAmountShow = itemRepercussionApply.packSize === '2' ?
                        itemRepercussionApply.remainingAmount / itemRepercussionApply.packageNum : itemRepercussionApply.remainingAmount

                    // 明细 退药数量显示 如果是医嘱开的是大包装，除以包装数量
                    // 明细 returnAmount 退药数量 默认为小包装
                    itemRepercussionApply.returnAmountShow = itemRepercussionApply.packSize === '2' ?
                        itemRepercussionApply.returnAmount / itemRepercussionApply.packageNum : itemRepercussionApply.returnAmount

                    // 明细 开药总数量显示 如果是医嘱开的是大包装，除以包装数量
                    // 明细 dispensing 退药数量 默认为小包装
                    itemRepercussionApply.dispensingAmountShow = itemRepercussionApply.packSize === '2' ?
                        itemRepercussionApply.dispensingAmount / itemRepercussionApply.packageNum : itemRepercussionApply.dispensingAmount

                    // 明细 退药后剩余数量显示   开药数量- 退药数量
                    itemRepercussionApply.remainingAmountShow = itemRepercussionApply.dispensingAmountShow - itemRepercussionApply.returnAmountShow
                    itemRepercussionApply.unitsName = item.unitsName
                    itemRepercussionApply.itemSpec = item.itemSpec
                })
            }
        })
    });
}

defineExpose({
    initOpen
})
</script>


<style scoped>
:deep(.el-table .success-row) {
    --el-table-tr-bg-color: var(--el-color-success-light-9);
}

</style>
